﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using Microsoft.Practices.Composite.Events;
using Microsoft.Practices.Composite.Presentation.Commands;
using Silverlight.Weblog.Client.DAL.Events;
using Silverlight.Weblog.Client.Default.Widgets.ViewModels;
using Silverlight.Weblog.Common.IoC;

namespace Silverlight.Weblog.Client.Default.Widgets
{
    public class MultipleBlogPostItemViewModel : BlogPostViewModel, IMultipleBlogPostItemViewModel
    {
        public MultipleBlogPostItemViewModel()
        {
            IsDisplayedInMultiplePanel = true;

            ExapndCommentButton = new DelegateCommand<object>(ExpandComments);
        }


        #region CommentsControl
        private CommentWidget _commentsControl = null;
        /// <summary>
        /// Only used in multiple blog post view states. 
        /// </summary>
        public CommentWidget CommentsControl
        {
            get { return _commentsControl; }
            set
            {
                if (_commentsControl != value)
                {
                    _commentsControl = value;
                    RaisePropertyChanged("CommentsControl");
                }

            }
        }
        #endregion CommentsControl

        public ICommand ExapndCommentButton { get; set; }
        private void ExpandComments(object obj)
        {
            _isExpanded = !_isExpanded;

            if (_isExpanded)
            {
                if (CommentsControl == null)
                    CommentsControl = new CommentWidget(this.Model);

                VSMState = "Expanded";
            }
            else
            {
                VSMState = "Collapsed";
            }
            IoC.Get<IEventAggregator>().GetEvent<CommentsExpandedEvent>().Publish(this);
        }

        private bool _isExpanded = false;

        #region VSMState
        private string _vSMState;
        public string VSMState
        {
            get { return _vSMState; }
            set
            {
                if (_vSMState != value)
                {
                    _vSMState = value;
                    RaisePropertyChanged("VSMState");
                }

            }
        }
        #endregion VSMState

        protected override void OnModelChanged()
        {
            if (CommentsControl != null)
            {
                CommentsControl.BlogPostSetter = this.Model;
            }
        }
    }
}